package com.loong.ems.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.loong.ems.domain.entity.Employee;
import com.loong.ems.domain.vo.resp.EmployeeExcelVo;
import com.loong.ems.mapper.EmployeeMapper;
import com.loong.ems.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 龙勇-229970722
 * @date 2025-01-02 9:11
 * @description: 接口实现
 */

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 查询所有员工信息
     * @return
     */
    @Override
    public List<Employee> list() {
        return employeeMapper.list();
    }

    /**
     * 离职员工
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        employeeMapper.deleteById(id);
    }

    /**
     * 更新员工信息
     * @param employee
     */
    @Override
    public void updateByEmployee(Employee employee) {
        employeeMapper.updateByEmployee(employee);
    }

    /**
     * 新增员工信息
     * @param employee
     */
    @Override
    public void addByEmployee(Employee employee) {
        employeeMapper.addByEmployee(employee);

    }

    /**
     * 分页查询员工信息
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @return
     */
    @Override
    public PageInfo<Employee> listByPage(int pageNum, int pageSize) {
        // 使用 PageHelper 分页
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> employees = employeeMapper.list();
        return new PageInfo<>(employees);
    }

    /**
     * 查询所有离职员工信息
     * @return
     */
    @Override
    public List<Employee> listLeave() {
        return employeeMapper.listLeave();
    }

    /**
     * 恢复员工信息
     * @param id
     */
    @Override
    public void recover(Long id) {
        employeeMapper.recover(id);
    }

    /**
     * 搜索员工信息模糊查询
     * @param str   关键字
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @return
     */
    @Override
    public List<Employee> search(String str, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> employees = employeeMapper.search(str);
        return employees;
    }

    /**
     * 查询员工总数
     * @return
     */
    @Override
    public int count() {
        return employeeMapper.count();
    }

    /**
     * 导出员工信息到 Excel 文件
     * @param filePath
     */
    public void exportEmployeeData(String filePath) {
        List<EmployeeExcelVo> employeeList = employeeMapper.getAllEmployeeData();  // 获取数据
        EasyExcel.write(filePath, EmployeeExcelVo.class)  // 写入 Excel
                .sheet("员工信息")  // 工作表名称
                .doWrite(employeeList);
    }

}
